package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 配置文件
 * 配置死信队列
 *  一个正常交换机
 *  一个死信队列
 *  一个正常队列
 *  将上面进行绑定
 */
@Configuration
public class DeadLetterMqConfig {


    //一个正常交换机
    public static final String exchange_dead = "exchange.dead";
   //一个正常交换机与一个死信队列 之间的routingKey
    public static final String routing_dead_1 = "routing.dead.1";
    //一个正常交换机与一个正常队列 之间的routingKey
    public static final String routing_dead_2 = "routing.dead.2";
    //一个死信队列
    public static final String queue_dead_1 = "queue.dead.1";
    //一个正常队列
    public static final String queue_dead_2 = "queue.dead.2";



    //一个正常交换机
    @Bean
    public Exchange exchange(){
        return ExchangeBuilder.directExchange(exchange_dead).build();
    }
    //一个死信队列
    @Bean
    public Queue deadQueue1(){
        Map<String, Object> arguments = new HashMap<>();
        //死信参数
        arguments.put("x-dead-letter-exchange", exchange_dead);
        arguments.put("x-dead-letter-routing-key", routing_dead_2);
        arguments.put("x-dead-letter-ttl", 5000);//全局设置死信 过期时间  优先级低于发消息（指定过期时间）
        return QueueBuilder.durable(queue_dead_1).withArguments(arguments).build();
    }
    //绑定交换机与死信队列
    @Bean
    public Binding bindingDeadQueue1ToExchange(){
        return BindingBuilder.bind(deadQueue1()).to(exchange()).with(routing_dead_1).noargs();
    }
    //一个正常队列
    @Bean
    public Queue deadQueue2(){
        return QueueBuilder.durable(queue_dead_2).build();
    }
    //绑定正确队列 与正常交换机
    @Bean
    public Binding bindingDeadQueue2ToExchange(){
        return BindingBuilder.bind(deadQueue2()).to(exchange()).with(routing_dead_2).noargs();
    }







}
